//lockbox.txt - A locked box.
//Cell 0 - Difficulty of lock.
//Cell 1 - The required key. If 0, none. Otherwise, the number of the special item 
//required to open the door.
//Cell 2,3 - The sdf which is set to 1 when box is unlocked.

beginobjectscript; // basic box

variables;
	short i_am_open = 0;
	short i_am_unlocked = 0;

body;

beginstate INIT_STATE;
	set_obj_tool_difficulty(ME,get_memory_cell(0));
	if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0)) {
		if (get_sdf(get_memory_cell(2),get_memory_cell(3)) > 0) {
			i_am_unlocked = 1;
			set_obj_tool_difficulty(ME,0);
			}
		}
	break;

beginstate DEAD_STATE;

break;

beginstate START_STATE;

break;

beginstate USE_STATE;
	if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0)) {
		if (get_sdf(get_memory_cell(2),get_memory_cell(3)) > 0)
			i_am_unlocked = 1;
		}
		
	if ((get_memory_cell(1) > 0) && (get_memory_cell(0) > 0) && (i_am_unlocked == 0)) {
		if (has_spec_item(get_memory_cell(1))) {
			print_str("Open Box: One of your keys unlocks the box. You open it.");
			i_am_unlocked = 1;
			play_sound(171);
			}

		}

	if ((get_memory_cell(0) > 0) && (get_obj_tool_difficulty(ME) > 0) && (i_am_unlocked == 0)) {
		i_am_unlocked = 0;
		if (get_obj_num_tools_needed(ME) <= 0) {
			print_str("Open Box: You manage to pick the lock.");
			award_party_xp(BASE_TRAP_XP,trap_lev_to_xp_lev(get_memory_cell(0)));
			i_am_unlocked = 1;		
			play_sound(171);
			}
			else {
				if (num_of_item(255) < get_obj_num_tools_needed(ME)) {
					if (get_memory_cell(1) > 0) {
						print_str_color("Open Box: The box is locked. You don't have the key, and",2);
						print_big_str_color("  you don't have enough Living Tools to get it open. (You need ",get_obj_num_tools_needed(ME),".)",2);
						}
						else {
							print_str_color("Open Box: The box is locked, and you don't have enough",2);
							print_big_str_color("  Living Tools to get it open. (You need ",get_obj_num_tools_needed(ME),".)",2);
							}
					end();
					}
				if (get_memory_cell(1) > 0) 
					show_object_disarm_window(ME,"The box is locked, and you don't have the key. However, you could use Living Tools to attempt to open it.");
					else show_object_disarm_window(ME,"The box is locked. You could use Living Tools to attempt to open it.");	
				}
		}
	
	if (i_am_unlocked == 0)
		end();
	if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0))
		set_flag(get_memory_cell(2),get_memory_cell(3),1);
		
	if (i_am_open == 0) {
		i_am_open = 1;
		open_container();
		}
		else {
			close_container();
			i_am_open = 0;
			}
break;

beginstate TOOLS_USED_STATE;
	if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0)) {
		if (get_flag(get_memory_cell(2),get_memory_cell(3)) == 0)
			award_party_xp(BASE_TRAP_XP,trap_lev_to_xp_lev(get_memory_cell(0)));
		set_flag(get_memory_cell(2),get_memory_cell(3),1);
		}

	print_str_color("Open Box: You unlock and open the box.",2);
	pc_heard_sound(171);
	i_am_open = 1;
	open_container();
break;

beginstate UNLOCK_STATE;
	if ((i_am_unlocked > 0) || (get_obj_tool_difficulty(ME) == 0)) {
		print_str("Unlock Spell: The box is already unlocked.");
		end();
		}

	if (current_unlock_strength() >= get_memory_cell(0)) {
		print_str("Unlock Spell: The spell loosens the lock. The box is unlocked.");
		i_am_unlocked = 1;
			play_sound(171);
		if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0))
			set_flag(get_memory_cell(2),get_memory_cell(3),1);
		end();
		}
	print_str("Unlock Spell: The spell is too weak to affect the lock.");	
break;

